﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class BFileUploadOutput:ResultBase
    {
        #region props
        private int _TotalFileUploaded;
        /// <summary></summary>
        public int TotalFileUploaded
        {
            get
            {
                return this._TotalFileUploaded;
            }
            set
            {
                this._TotalFileUploaded = value;
            }
        }

        private string _LogFilePath;
        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this._LogFilePath;
            }
            set
            {
                this._LogFilePath = value;
            }
        }

        private string _ErrorFilePath;
        /// <summary></summary>
        public string ErrorFilePath
        {
            get
            {
                return this._ErrorFilePath;
            }
            set
            {
                this._ErrorFilePath = value;
            }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public BFileUploadOutput():base(Guid.Empty,Guid.Empty)
        {
            this._TotalFileUploaded = 0;
            this._LogFilePath = string.Empty;
            this._ErrorFilePath = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="totalFileUploaded"></param>
        /// <param name="logFilePath"></param>
        /// <param name="errorFilePath"></param>
        public BFileUploadOutput(Guid componentID, Guid jobTicketID,
            int totalFileUploaded, 
            string logFilePath,
            string errorFilePath) 
            : base(componentID, jobTicketID)
        {
            this._TotalFileUploaded = totalFileUploaded;
            this._LogFilePath = logFilePath;
            this._ErrorFilePath = errorFilePath;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TotalFileUploaded", this.TotalFileUploaded.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath", this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrorFilePath", this.ErrorFilePath);

            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                BFileUploadOutput output = new BFileUploadOutput();
                // output.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.TotalFileUploaded = int.Parse(xNode.Attributes.GetNamedItem("TotalFileUploaded").Value);
                output.LogFilePath = xNode.Attributes.GetNamedItem("LogFilePath").Value;
                output.ErrorFilePath = xNode.Attributes.GetNamedItem("ErrorFilePath").Value;
                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}
